<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 100px;
            height: 100px;
            background-color: aqua;
            position: absolute;
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <script>
        function DragBox(ele) {
            this.el = document.querySelector(ele);

            this.dragStart = function () {
                let self = this;
                this.el.onmousedown = function (evt) {
                    var e = evt || window.event;
                    var disX = e.pageX - this.offsetLeft;
                    var disY = e.pageY - this.offsetTop;
                    self.dragIng(disX, disY);
                    self.dragEnd();
                }
            }
            this.dragIng = function (x, y) {
                document.onmousemove = (evt) => {
                    var e = evt || window.event;
                    this.el.style.left = e.pageX - x + "px";
                    this.el.style.top = e.pageY - y + "px";
                }
            }
            this.dragEnd = function () {
                document.onmouseup = function () {
                    document.onmousemove = document.onmouseup = null;
                }
            }
        }
        new DragBox(".box").dragStart();
    </script>
</body>
</html>